package view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.Socket;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.ConnectionHandler;
import net.EncryptedConnectionHandler;
import net.MessageReceivedListener;
import net.StandardConnectionHandler;
import net.miginfocom.swing.MigLayout;
import types.LogOnArguments;
import types.Message;
import types.MessageType;


public class EloEsoChatDialog extends JFrame {

	private JTextField textField;
	private JTextArea textArea;
	private JScrollPane scrollPane;
	
	private Socket soc;
	private String nick;
	private ConnectionHandler handler;
	
	/**
	 * Create the application.
	 */
	public EloEsoChatDialog(Socket soc, LogOnArguments args) {
		
		initialize();
		
		this.soc = soc;
		this.nick = args.getNick();
		
		if(args.getKey() == null) {
			handler = new StandardConnectionHandler(soc, new MessageReceivedListener() {
				
				@Override
				public void messageReceived(Message message) {
					
					showMessage(message);
				}
			});
		}
		else
		{
			try {
				handler = new EncryptedConnectionHandler(soc, new MessageReceivedListener() {
					
					@Override
					public void messageReceived(Message message) {
						
						showMessage(message);
					}
				},
				args.getKey());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void showMessage(Message message) {
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("hh:mm:ss"); 
		
		textArea.append(message.getCreator() + " (" + simpleDateFormat.format(message.getDate()) + ") \n");
		textArea.append(message.getMessage() + "\n");
		textArea.append("-------------------------------\n");
		
		textArea.setCaretPosition(textArea.getText().length());
	}
	
	private void sendMessage() {
		
		String text = textField.getText();
		if(!text.isEmpty()) {
			send(text);
		}
	}
	
	private void send(String text) {
		try {
			handler.sendMessage(new Message(text,  nick, MessageType.MESSAGE));
			textField.setText("");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		setTitle("EloEsoChat");
		setBounds(100, 100, 450, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(new MigLayout("", "[grow]", "[grow][]"));
		
		textField = new JTextField();
		textField.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER) {
					sendMessage();
				}
			}
		});
			
		scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, "cell 0 0,grow");
		
		textArea = new JTextArea();
		textArea.setTabSize(10);
		textArea.setEditable(false);
		scrollPane.setViewportView(textArea);
		
		getContentPane().add(textField, "flowx,cell 0 1,growx");
		textField.setColumns(10);
		
		JButton btnSend = new JButton("Wyslij");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendMessage();
			}
		});
		getContentPane().add(btnSend, "cell 0 1");
		
		
	}
}
